<?php 
function tos_unregister_GLOBALS() {
    if ( ! ini_get('register_globals')){
        return;
    }

    if (isset($_REQUEST['GLOBALS'])) {
        die( 'GLOBALS overwrite attempt detected');
    }
    
    // #Variables that shouldn't be unset
    $no_unset = array( 'GLOBALS', '_GET', '_POST', '_COOKIE', '_REQUEST', '_SERVER', '_ENV', '_FILES');

    $input = array_merge($_GET, $_POST, $_COOKIE, $_SERVER, $_ENV, $_FILES, isset( $_SESSION ) && is_array( $_SESSION ) ? $_SESSION : array());
    foreach ($input as $k => $v ) {
        if ( ! in_array( $k, $no_unset ) && isset($GLOBALS[$k])) {
            $GLOBALS[$k] = null;
            unset( $GLOBALS[$k] );
        }
    }
}


function tos_fix_server_vars() {
    global $PHP_SELF;

    $default_server_values = array(
        'SERVER_SOFTWARE' => '',
        'REQUEST_URI' => '',
    );

    $_SERVER = array_merge( $default_server_values, $_SERVER );

    // Fix for IIS when running with PHP ISAPI
    if ( empty( $_SERVER['REQUEST_URI'] ) || ( php_sapi_name() != 'cgi-fcgi' && preg_match( '/^Microsoft-IIS\//', $_SERVER['SERVER_SOFTWARE'] ) ) ) {

        // IIS Mod-Rewrite
        if ( isset( $_SERVER['HTTP_X_ORIGINAL_URL'] ) ) {
            $_SERVER['REQUEST_URI'] = $_SERVER['HTTP_X_ORIGINAL_URL'];
        }
        // IIS Isapi_Rewrite
        else if ( isset( $_SERVER['HTTP_X_REWRITE_URL'] ) ) {
            $_SERVER['REQUEST_URI'] = $_SERVER['HTTP_X_REWRITE_URL'];
        } else {
            // Use ORIG_PATH_INFO if there is no PATH_INFO
            if ( !isset( $_SERVER['PATH_INFO'] ) && isset( $_SERVER['ORIG_PATH_INFO'] ) )
                $_SERVER['PATH_INFO'] = $_SERVER['ORIG_PATH_INFO'];

            // Some IIS + PHP configurations puts the script-name in the path-info (No need to append it twice)
            if ( isset( $_SERVER['PATH_INFO'] ) ) {
                if ( $_SERVER['PATH_INFO'] == $_SERVER['SCRIPT_NAME'] )
                    $_SERVER['REQUEST_URI'] = $_SERVER['PATH_INFO'];
                else
                    $_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'] . $_SERVER['PATH_INFO'];
            }

            // Append the query string if it exists and isn't null
            if ( ! empty( $_SERVER['QUERY_STRING'] ) ) {
                $_SERVER['REQUEST_URI'] .= '?' . $_SERVER['QUERY_STRING'];
            }
        }
    }

    // Fix for PHP as CGI hosts that set SCRIPT_FILENAME to something ending in php.cgi for all requests
    if ( isset( $_SERVER['SCRIPT_FILENAME'] ) && ( strpos( $_SERVER['SCRIPT_FILENAME'], 'php.cgi' ) == strlen( $_SERVER['SCRIPT_FILENAME'] ) - 7 ) )
        $_SERVER['SCRIPT_FILENAME'] = $_SERVER['PATH_TRANSLATED'];

    // Fix for Dreamhost and other PHP as CGI hosts
    if ( strpos( $_SERVER['SCRIPT_NAME'], 'php.cgi' ) !== false )
        unset( $_SERVER['PATH_INFO'] );

    // Fix empty PHP_SELF
    $PHP_SELF = $_SERVER['PHP_SELF'];
    if ( empty( $PHP_SELF ) )
        $_SERVER['PHP_SELF'] = $PHP_SELF = preg_replace( '/(\?.*)?$/', '', $_SERVER["REQUEST_URI"] );
}


function tos_favicon_request() {
    if ( '/favicon.ico' == $_SERVER['REQUEST_URI'] ) {
        header('Content-Type: image/vnd.microsoft.icon');
        header('Content-Length: 0');
        exit;
    }
}


function tos_set_internal_encoding() {
    if ( function_exists( 'mb_internal_encoding' ) ) {
        if ( !@mb_internal_encoding( get_option( 'web_charset' ) ) )
            mb_internal_encoding( 'UTF-8' );
    }
}


function tos_fix_gpc_magic() {
  if (ini_get('magic_quotes_gpc')) {
    array_walk($_GET, '_fix_gpc_magic');
    array_walk($_POST, '_fix_gpc_magic');
    array_walk($_COOKIE, '_fix_gpc_magic');
    array_walk($_REQUEST, '_fix_gpc_magic');
    array_walk($_FILES, '_fix_gpc_magic_files');
    $fixed = TRUE;
  }
}

function _fix_gpc_magic(&$item) {
  if (is_array($item)) {
    array_walk($item, '_fix_gpc_magic');
  }
  else {
    $item = stripslashes($item);
  }
}

function _fix_gpc_magic_files(&$item, $key) {
  if ($key != 'tmp_name') {
    if (is_array($item)) {
      array_walk($item, '_fix_gpc_magic_files');
    }
    else {
      $item = stripslashes($item);
    }
  }
}


function tos_load_setting() {
    static $loaded = false;
    if ($loaded) {
        return;
    }
    
    include SYS_DIR . '/config/setting.php';
    
    if (defined('SITE_DIR')) {
        $site_setting_file = SITE_DIR . '/config/setting.php';
        if (file_exists($site_setting_file)) {
            include $site_setting_file;
        }
    }
    
    $_tos_memory_limit = tos_get_config('memory_limit');
    if ( function_exists('memory_get_usage') && ( (int) @ini_get('memory_limit') < abs($_tos_memory_limit) ) )
        @ini_set('memory_limit', $_tos_memory_limit);
    
    $loaded = true;
}

function tos_debug_mode($debug = false) {
    if ($debug) {
        if (defined( 'E_DEPRECATED'))
            error_reporting(E_ALL & ~E_DEPRECATED & ~E_STRICT);
        else
            error_reporting(E_ALL);

        ini_set( 'display_errors', 1 );
        ini_set( 'log_errors', 1 );
        ini_set( 'error_log', SYS_DIR . '/log/debug.log' );
        Db_mysqli::$IS_DEBUG = true;
        
    } else {
        if ( defined( 'E_RECOVERABLE_ERROR' ) )
            error_reporting( E_CORE_ERROR | E_CORE_WARNING | E_COMPILE_ERROR | E_ERROR | E_WARNING | E_PARSE | E_USER_ERROR | E_USER_WARNING | E_RECOVERABLE_ERROR );
        else
            error_reporting( E_CORE_ERROR | E_CORE_WARNING | E_COMPILE_ERROR | E_ERROR | E_WARNING | E_PARSE | E_USER_ERROR | E_USER_WARNING );
    }
}
?>